<?php

class Controller_Admin_Faq_Edit extends Controller_Admin_Abstract
{
	function execute($request)
	{

		$form = new Form_Admin_Faq_Edit;

		$id = $request -> getParam('_id');

		$item = null;

		if ($id)
		{
			$item = Mongo_Document::factory('faq', $id);
		}

		if ($id && $request -> isGet())
		{
			$item -> load();
			$form -> setValues($item -> as_array());
		}

		if ($request -> isPost() && $form -> isValid($_POST))
		{
			$data = $form -> getValues();

			$item -> question = $data['question'];
			$item -> answer = $data['answer'];
			$item->updated_at = time();

			$item -> save();

			Agilet::redirect('admin/faq', null, null);
		}

		$request -> response = View::render($data = array('form' => $form), $script = 'controller/admin/edit.tpl');
	}

}
